package com.zmzncs.lmtc.module.order.controller;


import com.fasterxml.jackson.databind.JsonNode;
import com.zmzncs.lmtc.common.pojo.miwang.*;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.interactive.miwang.MiWangAESUtil;
import com.zmzncs.lmtc.module.park.service.MiWangYunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 咪网云盒前端控制器
 * </p>
 *
 * @author JD
 * @since 2020-09-29
 */
@Api(tags = "咪网云盒管理")
@RestController
@RequestMapping("/tcjg/api")
@Validated
@Slf4j
public class  MiWangOrderController {

    @Autowired
    MiWangYunService miWangYunService;

    @ApiOperation(value = "车辆进场通知")
    @PostMapping(value = "uploadCarInData")
    public MiWangRest uploadCarInData(String accessID,String cipher,String sign) {
        String data = MiWangAESUtil.decryptByKey(cipher);
        log.info( "车辆进场通知 {} ", data );
        JsonNode jsonNode = JsonUtil.getJsonNode(data);
        String dataStr = jsonNode.get("data").toString();
        MWParkCarInFrom from = JsonUtil.JSONToObj(dataStr, MWParkCarInFrom.class);
        MiWangRest rest = miWangYunService.uploadCarInData(from);

        return rest;
    }

    @ApiOperation(value = "车辆出场通知")
    @PostMapping(value = "uploadCarOutData")
    public MiWangRest uploadCarOutData(String accessID,String cipher,String sign) {
        String data = MiWangAESUtil.decryptByKey(cipher);
        log.info( "车辆出场通知 {} ", data );
        JsonNode jsonNode = JsonUtil.getJsonNode(data);
        String dataStr = jsonNode.get("data").toString();
        MWParkCarOutFrom from = JsonUtil.JSONToObj(dataStr, MWParkCarOutFrom.class);
        MiWangRest rest = miWangYunService.uploadCarOutData(from);

        return rest;
    }


    @ApiOperation(value = "上传禁止放行")
    @PostMapping(value = "pushReleaseModeForbidden")
    public MiWangRest pushReleaseModeForbidden(String accessID,String cipher,String sign) {
        String data = MiWangAESUtil.decryptByKey(cipher);
        log.info( "上传禁止放行 {} ", data);
        JsonNode jsonNode = JsonUtil.getJsonNode(data);
        String dataStr = jsonNode.get("data").toString();
        MWPushReleaseModeForbiddenFrom from = JsonUtil.JSONToObj(dataStr, MWPushReleaseModeForbiddenFrom.class);
        MiWangRest rest = miWangYunService.pushReleaseModeForbidden(from);

        return rest;
    }


    @ApiOperation(value = "停车场基础信息变更")
    @PostMapping(value = "uploadParkingInfo")
    public MiWangRest uploadParkingInfo(String accessID,String cipher,String sign) {
        log.info( "停车场基础信息变更 {} ", accessID );
        String data = MiWangAESUtil.decryptByKey(cipher);
        JsonNode jsonNode = JsonUtil.getJsonNode(data);
        String dataStr = jsonNode.get("data").toString();
        MWUploadParkingInfoForm from = JsonUtil.JSONToObj(dataStr, MWUploadParkingInfoForm.class);
        MiWangRest rest = miWangYunService.uploadParkingInfo(from);

        return rest;
    }


    @ApiOperation(value = "上传停车场空闲状态")
    @PostMapping(value = "uploadParkingState")
    public MiWangRest uploadParkingState(String accessID,String cipher,String sign) {
        String data = MiWangAESUtil.decryptByKey(cipher);
        JsonNode jsonNode = JsonUtil.getJsonNode(data);
        String dataStr = jsonNode.get("data").toString();
        MWUploadParkingStateForm from = JsonUtil.JSONToObj(dataStr, MWUploadParkingStateForm.class);
        log.info( "停车场剩余车位数{}", from.getLeavNum());

        MiWangRest rest = miWangYunService.uploadParkingState(from);

        return rest;
    }

}

